//通用去对象
function $(e){
  return document.getElementById(e);
}

function $n(e)
{
 return document.getElementsByName(e);
}

function $t(e)
{
 return document.getElementsByTagName(e);
}


//删除文章
function deleteArticle(url){
  if(window.confirm("确定要删除吗？删除后数据不可恢复")) {
	location.href = url;
  }
}

//切换状态
function stateSwitch(url){
   if(window.confirm("确定要切换吗？切换后不可改变！")) {
	window.parent.con.location.href = url;
  }
}

//删除层
function none(e){
 var obj = $(e);
  obj.className = obj.className == "none" ? "show" : "none";
}

function wordNone(t , e){
 var obj = $(e);
 var con = window.parent.document.getElementById('con');
 t.className = t.className == 'word_none' ? 'word_show' : 'word_none';
 obj.className = obj.className == 'list' ? 'list none' :'list';
 con.style.height = document.body.offsetHeight;;
}

function ChildAll(e){
  var list , obj , i;
  obj = $(e);
  list = obj.getElementsByTagName("input");
  for ( i=0; i < list.length; i++  )
  {
	 if (list[i].type == "checkbox")
	 {
		 list[i].checked =  list[i].checked == true ? false : true;
	 }
  }
}

//删除层
function remove(e){
 if(e){
  $(e).style.display = "none";
 }
 if($("lockBg")){
  $('lockBg').parentNode.removeChild($("lockBg"));
 }
}

//显示层
function show(e){
 if(e){
  $(e).style.display = "block";
 }
}

//过滤空格
 String.prototype.Trim = function(){
  return this.replace(/(&nbsp;)|(^\s*)|(\s*)/g, "");
}
//锁屏
function jsLock(){
 var o = $('lockBg');
 if (!o){
  var obj = document.createElement("div");
  obj.setAttribute("id","lockBg");
  obj.setAttribute("className","lockBg");
  document.body.appendChild(obj);
 }
}


function openWindow(obj , power , typeid, level, qq, kpiType, entryDate, leaveDate){
	var win , radioObj , i;
	jsLock();
	win = $('lockScreen');
	win.style.display = "block";
	win.style.top = document.documentElement.scrollTop + 120; 

	$('userName').value = obj[1].innerHTML;
	$("userID").value = parseInt(obj[0].innerHTML);
	$('selectEdit').value = typeid;
	$('levelId').value=level;
	$('EditQQ').value=qq;
	$('kpiTypeId').value=kpiType;
	$('entryDateId').value=entryDate;
	$('leaveDateId').value=leaveDate;

	if (power == 2)
	{
	   radio = $n("power");
	   radio[parseInt(obj[3].flag)].checked = true
	}
}

//新建资源信息
function newResource(e){
  var i , obj;
  for (i = 0; i < e.length ; i++ )
  {
	 if (e.value == 0)
	 {
		 jsLock();
		 obj = $('newUSerBox');
		 obj.style.display = "block";
		 obj.style.top = document.documentElement.scrollTop + 200; 
		 break;
	 }
  }
}

//异步新建资源信息
function insertResource(type){
  var txt , url;
  txt = $('labelName');
  if (txt.value == ""){
	  alert("名称不能为空");
  }else{
	 url = "index.php?q=addDepartment&type="+type+"&name="+encodeURIComponent(txt.value);
     var obj = new Object();
	 obj.url = "okAction";
	 obj.id = '';
	 createRequest();
     startRequest(url, obj);
  }
}

//添加部门与资源后回调
function okAction(e){
 var select = $('selectData');
 var value = $('labelName').value;
 var result = parseInt(xmlhttp.responseText);
 if (result > 0){ 
    select.add(new Option(value , result));
    select.value = result;
    alert("添加成功");
    remove('newUSerBox');
 }
}

//新建用户
function newUSerInfo(){
   jsLock();
   $('newUSer').style.display = "block";
   $('n_userName').value = '';
   $('n_passName').value = '';
   $('n_qq').value = '';
}


//公用数据验证
function dataValid(e){
  var list = $(e).elements;
  for (var i =0; i <list.length ; i++ )
  {
	 if (list[i].type == "text")
	 {
		if(list[i].value == ""){
		  alert("必填项不能为空！");
		  return false;
		}
	 }
  }

}

//添加购物车
function detailsPage(id ,value){
 var url  , i , list ;
  url = "index.php?q=setUserAssgin/"+id+"/"+value;  
  createRequest();
  startRequest(url, "detailsReturn");
}

//跟进信息验证
function insertVerlid(){
 var html = window.Editor.HtmlEditor.document.getElementsByTagName("BODY")[0].innerHTML;
 if (html == "" ){
       alert("内容不能为空！");
	   return false;
 }
}

//验证工作报告
function summaryVerlid(){
 var html = window.Editor.HtmlEditor.document.getElementsByTagName("BODY")[0].innerHTML;
 if(html.Trim() == ""){
   alert("内容不能为空！");
   return false;
 }
}

//重置按钮
function formReset(name){
   document.getElementById(name).reset();
   window.Editor.AttachReset();
}


//创建Ajax对象
function createRequest(){
   if(window.ActiveXObject){
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   }else if(window.XMLHttpRequest){
    xmlhttp = new XMLHttpRequest();
   }
}

//发送请求
function startRequest(url,act){
 createRequest();
 var way , values;
 if(arguments.length ==3){
   way = "POST";
   values = arguments[2];
 }else{
   way = "GET";
   values = null;
 }
 xmlhttp.onreadystatechange = function(){response(act);}
 xmlhttp.open(way,url,true);
 xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
 xmlhttp.send(values);
}

//公用回调
function response(e ){
   if(xmlhttp.readyState == 4){
     if(xmlhttp.status == 200){
		  if(typeof(e) == "object"){
			  setTimeout(e.url+"('"+e.id+"')",0);
		  }else{
			  setTimeout(e+"()",0);
		  }
		 
	 }
  }
}

//分配权限回调
function detailsReturn(){
 var result ;
 result = parseInt(xmlhttp.responseText);
  if(result == 0) alert("取消成功");
  else if(result == 1) alert("设置成功");
}

//增加购物车回调
function ShoppingCallback(){
  var result = parseInt(xmlhttp.responseText);
  if(result == 0){
    window.status = "成功加入购物车";
  }else{
    window.status = "购物车已经有条"+ result +"记录";
  }
}


//选择日期跳转
function dateLocation(e){
 var v = $('dateInput').value;
 location.href = "index.php?q=wordSummaryList&browsing="+e+"&date="+v;
}

//显示部门树下的员工
function treeMenu(e){
  var even  = event.srcElement.tagName;
  if(even != "DIV") return;
  var obj = e.parentNode.getElementsByTagName("dl")[0];
  if (obj.className == "none")
  {
	obj.className = 'show';
	e.parentNode.className = 'open';
  }else{
	obj.className = 'none';
	e.parentNode.className = '';
  }
}

//异步购物车
function Shopping(e){
 var url = e.checked == false ?  "index.php?q=Shopping/0/"+parseInt(e.value) : "index.php?q=Shopping/1/"+parseInt(e.value) ;
  createRequest();
  startRequest(url, "ShoppingCallback");
}

//部门资源信息修改
function RsourceInfoBox(obj , type){  
   jsLock();
   var list = obj.parentNode.parentNode.childNodes;
   var win = $('newUSerBox');
   win.style.display = "block";
   $('formName').action = 'index.php?q=editOtterInfo&id='+parseInt(type);
   win.style.top = document.documentElement.scrollTop + 120; 
   $('formId').value = parseInt(list[0].innerHTML);
   $('labelName').value = list[1].innerHTML;
}

//添加资源与部门
function addRDData(id){
   jsLock();
   var win = $('newUSerBox');
   win.style.display = "block";
   win.style.top = document.documentElement.scrollTop + 120; 
   $('formName').action = 'index.php?q=editOtterInfo&type=1&id='+parseInt(id);
}


//打开修改密码窗口
function passWindow(){
  var str= showModalDialog('index.php?q=changePass','widnow','dialogHeight: 220px; dialogWidth:302px; status:no; scroll:no');
}

//异步验证老密码
function changePass(values){ 
  if(values != ""){
   var url = "index.php?q=ChangePassValid";
   var values = "value="+encodeURIComponent(values);
   createRequest();
   startRequest(url, "callPass" ,values);
 }else{
   $('old_tip').className = 'err_info';
	old = 0;
 }
}

//验证密码回调
function callPass(){
  var result = parseInt(xmlhttp.responseText);
  if(result == 1){
     $('old_tip').className = 'ok_info';
	 old = 1;
  }else{
     $('old_tip').className = 'err_info';
	 old = 0;
  }
}


//isnull
function isNoNull(e){
  if(e.value == "") {$(e.flag).className = "err_info";}
  else {$(e.flag).className = "ok_info";}

}
//submit
function userPassValid(){
  if(old == 0){
    alert("旧密码输入错误！");
	return false;
  }else if(pass == 0){
    alert("密码不能为空或者输入不一致！");
	return false;
  }
  $('oldPass').disabled = true;
  $('newPass').disabled = true;
  $('confirmPass').disabled = true;
  $('submit_btn').disabled = true;
   var url = "index.php?q=ChangePassValid";
   var values = "pass="+encodeURIComponent($('newPass').value);
   createRequest();
   startRequest(url, "savaPassData" , values);
}

//重复密码验证
function dataIsNoNull(values){
  var txt = $('newPass').value;
  if(values !=  txt || txt == ''){
    $('comfirm_tip').className = 'err_info';
	pass = 0;
  }else{
    $('comfirm_tip').className = 'ok_info';
	pass = 1;
  }
}

//搜索下拉
function searchMenu(obj , str){
  var parents = obj.parentNode.childNodes;
  for(var i=0; i < parents.length; i++){
	parents[i].style.fontWeight = 400;
  }
  obj.style.fontWeight = 600;
  $('typeId').value = str;
  $('searchMenu').style.display = 'none';
}

//提交搜索条件
function searchSubmit(){
 var title = $('title').value;
 if(title == ""){
   alert("关键字不能为空"); 
   return false;
 }
 var url = 'index.php?q=reportedList&typeName='+$('typeId').value+'&title='+encodeURIComponent(title);
 location.href = url;
}

//查询相关主题
function selectRelatedTitle(str){
 if(str == "") return false;
  var url = 'index.php?q=viewRelatedTitle&title='+encodeURIComponent(str);
  createRequest();
  startRequest(url, "relatedXMl");
}

function relatedXMl(){
  var result , list , i , li , parents , obj;
  result = xmlhttp.responseXML.documentElement;
  if(result.hasChildNodes){
  list = result.childNodes;
  obj = $('titleList');
  obj.innerHTML = '';
  obj.parentNode.style.display = 'block';
  parents = document.createElement("ol");
  for (i = 0; i < list.length ; i++ )
  {
	 li = document.createElement("li");
	 li.innerHTML = '<a href=?q=reportedDetails/'+list[i].getAttribute("id")+'>'+list[i].text+'</a>';
     parents.appendChild(li);
  }
  obj.appendChild(parents);
  var hei = document.body.offsetHeight;
  var con = window.parent.document.getElementById('con');
  con.style.height = hei
  }
}

function savaPassData(){
  var result = parseInt(xmlhttp.responseText);
  if(result == 1){
	alert("修改成功！");
    window.top.close();   
  }
}


function userListSubmit(){
 var list = $n('userList');
 var values = "";
 for(var i=0; i < list.length; i++){
   if(list[i].checked == true){
    values += list[i].value+",";
   }
 }
 returnValue =values;
 window.top.close();
}

String.prototype.trim = function(){
	return this.replace(/(&nbsp;)|(^\s*)|(\s*)/g, "");
}
